{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Modules and Packages \n",
    "\n",
    "- Modules are the third party programs which u can import in your programs \n",
    "- A group of Modules are known as Packages \n",
    "- A module may contain Functions, Classes and Differnet programs \n",
    "\n",
    "- You can import Libray or modules from pypi.org\n",
    "- U can install it using pip install \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: docx in /opt/anaconda3/lib/python3.7/site-packages (0.2.4)\n",
      "Requirement already satisfied: Pillow>=2.0 in /opt/anaconda3/lib/python3.7/site-packages (from docx) (7.1.1)\n",
      "Requirement already satisfied: lxml in /opt/anaconda3/lib/python3.7/site-packages (from docx) (4.4.1)\n",
      "\u001b[33mWARNING: You are using pip version 20.0.2; however, version 20.1.1 is available.\n",
      "You should consider upgrading via the '/opt/anaconda3/bin/python -m pip install --upgrade pip' command.\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install docx"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Writing our OWN Libs "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing moduleOne.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile moduleOne.py\n",
    "\n",
    "def printTopTenEven():\n",
    "    for i in range(1,11):\n",
    "        if i%2 == 0 :\n",
    "            print(i)\n",
    "            \n",
    "def printTopTenOdd():\n",
    "    for i in range(1,11):\n",
    "        if i % 2 == 1:\n",
    "            print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import moduleOne"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "moduleOne.printTopTenEven()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "moduleOne.printTopTenOdd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<module 'moduleOne' from '/Users/nvrsettle/LU B4 - Python/moduleOne.py'>\n"
     ]
    }
   ],
   "source": [
    "#DEtails about Module One Library which we created \n",
    "\n",
    "print(moduleOne)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'printTopTenEven', 'printTopTenOdd']\n"
     ]
    }
   ],
   "source": [
    "t = dir(moduleOne)\n",
    "print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'pi', 'pow', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']\n"
     ]
    }
   ],
   "source": [
    "s= dir(math)\n",
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# How to create a Pacakge "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing moduleTwo.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile moduleTwo.py\n",
    "\n",
    "def printTopHundredEven():\n",
    "    for i in range(1,101):\n",
    "        if i%2 == 0 :\n",
    "            print(i)\n",
    "            \n",
    "def printTopHundredOdd():\n",
    "    for i in range(1,101):\n",
    "        if i % 2 == 1:\n",
    "            print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "---------\n",
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "---------\n",
      "2\n",
      "4\n",
      "6\n",
      "8\n",
      "10\n",
      "12\n",
      "14\n",
      "16\n",
      "18\n",
      "20\n",
      "22\n",
      "24\n",
      "26\n",
      "28\n",
      "30\n",
      "32\n",
      "34\n",
      "36\n",
      "38\n",
      "40\n",
      "42\n",
      "44\n",
      "46\n",
      "48\n",
      "50\n",
      "52\n",
      "54\n",
      "56\n",
      "58\n",
      "60\n",
      "62\n",
      "64\n",
      "66\n",
      "68\n",
      "70\n",
      "72\n",
      "74\n",
      "76\n",
      "78\n",
      "80\n",
      "82\n",
      "84\n",
      "86\n",
      "88\n",
      "90\n",
      "92\n",
      "94\n",
      "96\n",
      "98\n",
      "100\n",
      "---------\n",
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n",
      "11\n",
      "13\n",
      "15\n",
      "17\n",
      "19\n",
      "21\n",
      "23\n",
      "25\n",
      "27\n",
      "29\n",
      "31\n",
      "33\n",
      "35\n",
      "37\n",
      "39\n",
      "41\n",
      "43\n",
      "45\n",
      "47\n",
      "49\n",
      "51\n",
      "53\n",
      "55\n",
      "57\n",
      "59\n",
      "61\n",
      "63\n",
      "65\n",
      "67\n",
      "69\n",
      "71\n",
      "73\n",
      "75\n",
      "77\n",
      "79\n",
      "81\n",
      "83\n",
      "85\n",
      "87\n",
      "89\n",
      "91\n",
      "93\n",
      "95\n",
      "97\n",
      "99\n",
      "---------\n"
     ]
    }
   ],
   "source": [
    "# Here we have a package \n",
    "\n",
    "from LuModule import moduleOne\n",
    "from LuModule import moduleTwo\n",
    "\n",
    "moduleOne.printTopTenEven()\n",
    "print(\"---------\")\n",
    "moduleOne.printTopTenOdd()\n",
    "print(\"---------\")\n",
    "\n",
    "moduleTwo.printTopHundredEven()\n",
    "print(\"---------\")\n",
    "moduleTwo.printTopHundredOdd()\n",
    "print(\"---------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Difference B/W Name and Main \n",
    "\n",
    "\n",
    "When ever u want to find out is my .py file is getting called directly or in-directly that time we use __name__ == \"__main__\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting LuModule/moduleOne.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile LuModule/moduleOne.py\n",
    "\n",
    "def printTopTenEven():\n",
    "    for i in range(1,11):\n",
    "        if i%2 == 0 :\n",
    "            print(i)\n",
    "            \n",
    "def printTopTenOdd():\n",
    "    for i in range(1,11):\n",
    "        if i % 2 == 1:\n",
    "            print(i)\n",
    "            \n",
    "if __name__ == \"__main__\":\n",
    "    print(\"I am running directly I will shout on Sai\")\n",
    "else:\n",
    "    print(\"I am running in-directly I will shout on You all\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Hey, LetsUpgraders Value inside -  __main__\r\n",
      "I am running directly I will shout on Sai\r\n"
     ]
    }
   ],
   "source": [
    "! python LuModule/moduleOne.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting callPython.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile callPython.py\n",
    "from LuModule import moduleOne\n",
    "\n",
    "print(\"I am CallPython\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " Hey, LetsUpgraders Value inside -  LuModule.moduleOne\r\n",
      "I am running in-directly I will shout on You all\r\n",
      "I am CallPython\r\n"
     ]
    }
   ],
   "source": [
    "!python callPython.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# __Name__ == \"__main__\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# __name__ is a by default varibale in Python which holds the value weather\n",
    "# my program is getting called as Main program or support program "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting hi.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile hi.py\n",
    "print(\"HI\")\n",
    "\n",
    "print(\"Hi.py __name__= \",__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI\r\n",
      "Hi.py __name__=  __main__\r\n"
     ]
    }
   ],
   "source": [
    "! python hi.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting bye.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile bye.py\n",
    "import hi\n",
    "\n",
    "print(\"We are inside Bye\")\n",
    "print(\"Bye.py is running - \",__name__)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI\r\n",
      "Hi.py __name__=  hi\r\n",
      "We are inside Bye\r\n",
      "Bye.py is running -  __main__\r\n"
     ]
    }
   ],
   "source": [
    "! python bye.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Decorator "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#Problem - \n",
    "1. We have one function inside our .py file \n",
    "2. we dont want to rewrite the functuon \n",
    "3. still we want to modify the way function is working \n",
    "4. How is it possible ?\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Basic Function \n",
    "\n",
    "def hello():\n",
    "    print(\"Hi LetsUpgraders\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi LetsUpgraders\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Basic Function with one parameter\n",
    "\n",
    "def hello(name=\"Everyone\"):\n",
    "    print(\"Hi LetsUpgraders - \", name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi LetsUpgraders -  shreyas borse\n"
     ]
    }
   ],
   "source": [
    "hello(\"shreyas borse\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi LetsUpgraders -  Everyone\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "# what are locals and globals \n",
    "abc = \"Dakesh\"\n",
    "def hello(name=\"Everyone\"):\n",
    "    efg = \"Raja\"\n",
    "    print(\"Hi LetsUpgraders - \", name)\n",
    "    print(locals())\n",
    "    print(globals()[\"abc\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi LetsUpgraders -  Everyone\n",
      "{'name': 'Everyone', 'efg': 'Raja'}\n",
      "Dakesh\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "int"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Can we assing Function to a varibale ?\n",
    "abc = 10\n",
    "type(abc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "hi = hello"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hi LetsUpgraders -  rahul\n",
      "{'name': 'rahul', 'efg': 'Raja'}\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "hi(\"rahul\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "function"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(hi)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Can we call a function inside a function "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "def school():\n",
    "    print(\"I am inside school function\")\n",
    "    hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am inside school function\n",
      "Hi LetsUpgraders -  Everyone\n",
      "{'name': 'Everyone', 'efg': 'Raja'}\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "school()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can we pass function as a Argument inside another function ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "def namaste(temp):\n",
    "    print(\"Namaste Krishna\")\n",
    "    temp(\"Krishna\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namaste Krishna\n",
      "Hi LetsUpgraders -  Krishna\n",
      "{'name': 'Krishna', 'efg': 'Raja'}\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "namaste(hello)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "# can we define function inside function ??\n",
    "\n",
    "def vanakam():\n",
    "    print(\"HI Vanakam\")\n",
    "    def Namaskaram():\n",
    "        print(\"we are inside Namaskaram\")\n",
    "    #call the function\n",
    "    Namaskaram()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI Vanakam\n",
      "we are inside Namaskaram\n"
     ]
    }
   ],
   "source": [
    "vanakam()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "HI Vanakam\n",
      "we are inside Namaskaram\n"
     ]
    }
   ],
   "source": [
    "vanakam()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Can we return a Function from a Function ?\n",
    "\n",
    "def NamasteBhava():\n",
    "    print(\"We are in marathi Greetings\")\n",
    "    def Namaskara():\n",
    "        print(\"We are inside Kannada Greetings\")\n",
    "    return Namaskara"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are in marathi Greetings\n"
     ]
    }
   ],
   "source": [
    "kannadGreetings = NamasteBhava()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "We are inside Kannad Greetings\n"
     ]
    }
   ],
   "source": [
    "kannadGreetings()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simpleFunction():\n",
    "    print(\"I am simple Function\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am simple Function\n"
     ]
    }
   ],
   "source": [
    "simpleFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [],
   "source": [
    "#decorator --\n",
    "\n",
    "def ModifiedFunction(temp):\n",
    "    def newFunction():\n",
    "        print(\"You are inside a modified function \")\n",
    "        temp()\n",
    "        print(\"You are inside a modified function\")\n",
    "    return newFunction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am simple Function\n"
     ]
    }
   ],
   "source": [
    "simpleFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "SimpleFunctionModified = ModifiedFunction(simpleFunction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are inside a modified function \n",
      "I am simple Function\n",
      "You are inside a modified function\n"
     ]
    }
   ],
   "source": [
    "SimpleFunctionModified()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I am simple Function\n"
     ]
    }
   ],
   "source": [
    "simpleFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [],
   "source": [
    "@ModifiedFunction\n",
    "def hey():\n",
    "    print(\"This is one more simple Funtion\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You are inside a modified function \n",
      "This is one more simple Funtion\n",
      "You are inside a modified function\n"
     ]
    }
   ],
   "source": [
    "hey()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Make a small Decorator to take inputs\n",
    "\n",
    "def takeInput(temp):\n",
    "    def returnFunction():\n",
    "        num = int(input(\"Enter Number 1 \"))\n",
    "        num2 = int(input(\"Enter Number 2 \"))\n",
    "        temp(num,num2)\n",
    "    return returnFunction\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "@takeInput\n",
    "def addition(num1,num2):\n",
    "    print(num1 + num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter Number 1 45\n",
      "Enter Number 2 56\n",
      "101\n"
     ]
    }
   ],
   "source": [
    "addition()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [],
   "source": [
    "@takeInput\n",
    "def  sub(num1,num2):\n",
    "    print(num1-num2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter Number 1 3\n",
      "Enter Number 2 2\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "sub()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generators "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isPalindrome(arg1):\n",
    "    arg2 = arg1\n",
    "    arg2 = list(arg2)\n",
    "    arg1 = list(arg1)\n",
    "    arg1.reverse()\n",
    "    if arg1 == arg2:\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 151,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isPalindrome(\"hih\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [\"names\",\"lu\",\"lol\",\"text\",\"young\",\"pip\",\"yes\",\"no\",\"k\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 152,
   "metadata": {},
   "outputs": [],
   "source": [
    "newList = []\n",
    "for item in lst:\n",
    "    if isPalindrome(item):\n",
    "        newList.append(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['lol', 'pip', 'k']"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "newList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [],
   "source": [
    "def isPalindrome1(lst):\n",
    "    for item in lst:\n",
    "        arg1 = item\n",
    "        arg2 = arg1\n",
    "        arg2 = list(arg2)\n",
    "        arg1 = list(arg1)\n",
    "        arg1.reverse()\n",
    "        if arg1 == arg2:\n",
    "            yield item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [],
   "source": [
    "neList = list(isPalindrome1(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['lol', 'pip', 'k']"
      ]
     },
     "execution_count": 143,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "neList"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<generator object isPalindrome1 at 0x1111a9350>"
      ]
     },
     "execution_count": 153,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "isPalindrome1(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Genrator define - \n",
    "\n",
    "#When ever u want multple varibale to be returned from \n",
    "#a function u use yield keyword and that is know as Generator \n",
    "\n",
    "#but whenever u want to use single variable return \n",
    "#u can use it with simple return statement "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
